Object-oriented programming techniques allow for the defining of “interfaces” by which objects can expose their functionality independently of the implementation of the functionality. In the C++ programming language, an interface is defined by an abstract class whose virtual functions are all pure. A pure virtual function is one that has no implementation in the class. Thus, an interface defines only the order of the virtual functions within the class and the signatures of the virtual functions, but not their implementations. The following is an example of an interface:
class IShape{virtual void draw(int x,y)=0;virtual void save(char filename)=0;virtual void clear(int x,y)=0;}This interface, named “IShape,” has three virtual functions: draw, save, and clear. The “=0” after the formal parameter list indicates that each virtual function is pure. Concepts of the C++ programming language that support object-oriented programming are described in “The Annotated C++ Reference Manual,” by Ellis and Stroustrup, published by Addison-Wesley Publishing Company in 1990, which is hereby incorporated by reference.
Once an interface is defined, programmers can write programs to access the functionality independent of the implementation. Thus, an implementation can be changed or replaced without having to modify the programs that use the interface. For example, the save function of the IShape interface may have an implementation that saves the shape information to a file on a local file system. Another implementation may save the shape information to a file server accessible via the Internet.
To ensure that an implementation provides the proper order and signatures of the functions of an interface, the class that implements the interfaces inherits the interface. The following is an example of a class that implements the IShape interface.
class Shape: IShape{virtual void save(char filename){. . .};virtual void clear(int x,y){. . .);virtual void draw(int x,y) {. . .};virtual void internal_save( ){. . .);int x;int y;}The first line of the class definition indicates by the “: IShape” that the Shape class inherits the IShape interface. The ellipses between the braces indicate source code that implements the virtual functions. The Shape class, in addition to providing an implementation of the three virtual functions of the IShape interface, also defines a new virtual function “internal_save,” which may be invoked by one of the implementations of the other virtual functions. The Shape class also has defined two integer data members, x and y.
Typical C++ compilers generate virtual function tables to support the invocation of virtual functions. When an object for a class is instantiated, such a C++ compiler generates a data structure that contains the data members of the object and that contains a pointer to a virtual function table. The virtual function table contains the address of each virtual function defined for the class. FIG. 1 illustrates a sample object layout for an object of the Shape class. The object data structure 101 contains a pointer to a virtual function table and the data members x and y. The virtual function table 102 contains an entry for each virtual function. Each entry contains the address of the corresponding virtual function. For example, the first entry in the virtual function table contains the address of the draw function 103. The order of the references in the virtual function table is the same as defined in the inherited interface even though the Shape class specifies these three functions in a different order. In particular, the reference to the draw function is first, followed by the references to the save and clear functions.
The C++ compiler generates code to invoke virtual functions indirectly through the virtual function table. The following is C++ code for invoking a virtual function.:Shape*pShape=new Shape;pShape−>save {);The pointer pShape points to an object in memory that is of the Shape class. When compiling this invocation of the virtual function, the compiler generates code to retrieve the address of the virtual function table from the object, to add to the retrieved address the offset of the entry in the virtual function table that contains the address of the function, and to then invoke the function at that calculated address.
The inheritance of interfaces allows for references to objects that implement the interfaces to be passed in an implementation independent manner. A routine that uses an implementation may define a formal argument that is a pointer to the IShape interface. The developer of the routine can be unaware that the implementation is actually the Shape class. To pass a reference to an object of the Shape class, a program that invokes the routine would type cast a pointer to the object of the Shape class to a pointer to the IShape interface. So long as the pointer points to a location that contains the address of the virtual function table and the virtual function table contains the entries in the specified order, the invoked routine can correctly access the virtual functions defined by the IShape interface:
Although the use of interfaces facilitates the development of programs that use and implement interfaces, difficulties do arise. The following C++ source code illustrates one of these difficulties.
class A{virtual void A1( )=0;}class B : A{virtual void B 1( )=0;}class AImp : A{virtual void A1( ){. . .};virtual void A2( ){. . .};}class BImp : AImp, B ({virtual void B1( ){. . .};}In this example, classes A and B are interfaces. Interface A defines virtual function A1, and interface B defines virtual function B1. Class AImp is an implementation of interface A and defines a new virtual function A2. Class BImp is an implementation of interface B that inherits the implementation of interface A, which is class AImp. Thus, class BImp inherits multiple classes: class AImp and interface B.
Some C++ compilers have the option of merging the virtual function tables of multiple base classes into a single virtual function table in the derived class or of generating separate virtual function tables for each base class. One difficulty arises if the virtual function tables of the base classes are merged in the derived class. FIG. 2 illustrates the difficulty when the virtual function tables are merged. The object layouts 201 and 202 illustrate the order of the virtual functions within the virtual function tables. (Since interfaces A and B are abstract classes, objects of these classes cannot actually be instantiated.) To allow type casting as described above, an implementation of interface B needs to provide a virtual function table with entries in the same order as shown in object layout 202, that is with the entry for virtual function A1 followed immediately by the entry for virtual function B1. A problem arises when the implementation of interface A, which is class AImp, defines a new virtual function, such as virtual function A2. The virtual function table of class AImp is shown by object layout 203. Object layout 204 illustrates the virtual function table for class BImp when the virtual function tables are merged. This virtual function table for class BImp illustrates the order of the entries to be virtual functions A1, A2, and B1. If a pointer to an object of class AImp is type cast to a pointer to interface B, then the virtual function table would not have the entries in the order as specified by the interface B. In particular, the entry for virtual function A1 is not immediately followed by the entry for virtual function B1. A C++ compiler could have reordered in entries in the virtual function table within object layout 204 to be virtual functions A1, B1, and A2. In which case, the type casting for interface B would work. However, the type casting from a pointer to a class BImp object to a class AImp object would be incorrect because the class AImp defines that the entry for virtual function A1 is immediately followed by the entry for virtual function A2. Thus, a C++ compiler cannot define a single virtual function table that satisfies the virtual function table ordering of both interface B and class AImp.
Although the use of the separate option for generating virtual function tables alleviates this difficulty, additional difficulties arise. FIG. 3 illustrates the object layouts with separate virtual function tables. In this example, the object layout 304 for class BImp has two virtual function tables. The first virtual function table has the order of entries to be virtual functions A1 and A2, which is consistent with the ordering for class AImp. The second virtual function table has the order of entries to be virtual functions A1 and B1, which is consistent with the ordering for interface B. The difficulty arises because the developer of class BImp, who wants to reuse the implementation of interface A, which is class AImp, must provide an implementation for each virtual function of interface A within class BImp. (These multiple implementations can be avoided by virtually inheriting interface A. With such virtual inheritance, a C++ compiler might generate no virtual function tables with the required ordering of the virtual functions.) The developer of class BImp can implement each virtual function of interface A in a straightforward manner. In particular, the developer can implement each virtual function to forward its invocation to the corresponding virtual function in the implementation of interface A, which is class AImp. The following is an example implementation of class BImp:
Class BImp : AImp, B{virtual void B 1( ){. . .};virtual void A 1( ){ return AImp::A1( )}}The implementation of virtual function A1 invokes the implementation of virtual function A1 in the class AImp. Although this implementation is straightforward, problems arise when interface A is changed. For example, if a new virtual function A3 is added to interface A, then the developer of the class BImp would need to change the class definition to include an implementation of the new virtual function A3. Although the changing of the class definition in this example may not be difficult, the changing of class definitions in systems that use complex inheritance hierarchies and many virtual functions can present difficulties. It would be desirable to have a technique which would avoid the difficulties of having to change class definitions when a new virtual function is added to an interface.